#ifndef SQLITE_EX
#define SQLITE_EX


#include "sqlite3.h"
/*
** A function to close the database.
**
** Call this function with a pointer to a structure that was previously
** returned from sqlite3_open() and the corresponding database will by closed.
**
** All SQL statements prepared using sqlite3_prepare() or
** sqlite3_prepare16() must be deallocated using sqlite3_finalize() before
** this routine is called. Otherwise, SQLITE_BUSY is returned and the
** database connection remains open.
*/
/*int sqlite3_close(sqlite3 *);*/
typedef int (*PFN_Sqlite3_close)(sqlite3 *);


/*
** Each entry in an SQLite table has a unique integer key.  (The key is
** the value of the INTEGER PRIMARY KEY column if there is such a column,
** otherwise the key is generated at random.  The unique key is always
** available as the ROWID, OID, or _ROWID_ column.)  The following routine
** returns the integer key of the most recent insert in the database.
**
** This function is similar to the mysql_insert_id() function from MySQL.
*/
//sqlite_int64 sqlite3_last_insert_rowid(sqlite3*);
typedef sqlite_int64 (*PFN_Sqlite3_last_insert_rowid)(sqlite3*);


/*
** This routine sets a busy handler that sleeps for a while when a
** table is locked.  The handler will sleep multiple times until 
** at least "ms" milleseconds of sleeping have been done.  After
** "ms" milleseconds of sleeping, the handler returns 0 which
** causes sqlite3_exec() to return SQLITE_BUSY.
**
** Calling this routine with an argument less than or equal to zero
** turns off all busy handlers.
*/
/*int sqlite3_busy_timeout(sqlite3*, int ms);*/
typedef int (*PFN_Sqlite3_busy_timeout)(sqlite3*, int ms);


/*
** Open the sqlite database file "filename".  The "filename" is UTF-8
** encoded for sqlite3_open() and UTF-16 encoded in the native byte order
** for sqlite3_open16().  An sqlite3* handle is returned in *ppDb, even
** if an error occurs. If the database is opened (or created) successfully,
** then SQLITE_OK is returned. Otherwise an error code is returned. The
** sqlite3_errmsg() or sqlite3_errmsg16()  routines can be used to obtain
** an English language description of the error.
**
** If the database file does not exist, then a new database is created.
** The encoding for the database is UTF-8 if sqlite3_open() is called and
** UTF-16 if sqlite3_open16 is used.
**
** Whether or not an error occurs when it is opened, resources associated
** with the sqlite3* handle should be released by passing it to
** sqlite3_close() when it is no longer required.
*/
//int sqlite3_open(
//	const char *filename,   /* Database filename (UTF-8) */
//	sqlite3 **ppDb          /* OUT: SQLite db handle */
//	);
//int sqlite3_open16(
//	const void *filename,   /* Database filename (UTF-16) */
//	sqlite3 **ppDb          /* OUT: SQLite db handle */
//	);

typedef int (*PFN_Sqlite3_open)(
								const char *filename,   /* Database filename (UTF-8) */
								sqlite3 **ppDb          /* OUT: SQLite db handle */
								);
typedef int (*PFN_Sqlite3_open16)(
								  const void *filename,   /* Database filename (UTF-16) */
								  sqlite3 **ppDb          /* OUT: SQLite db handle */
								  );

/*
** Return a pointer to a UTF-8 encoded string describing in english the
** error condition for the most recent sqlite3_* API call. The returned
** string is always terminated by an 0x00 byte.
**
** The string "not an error" is returned when the most recent API call was
** successful.
*/
/*const char *sqlite3_errmsg(sqlite3*);*/
typedef const char * (*PFN_Sqlite3_errmsg)(sqlite3*);

/*
** To execute an SQL query, it must first be compiled into a byte-code
** program using one of the following routines. The only difference between
** them is that the second argument, specifying the SQL statement to
** compile, is assumed to be encoded in UTF-8 for the sqlite3_prepare()
** function and UTF-16 for sqlite3_prepare16().
**
** The first parameter "db" is an SQLite database handle. The second
** parameter "zSql" is the statement to be compiled, encoded as either
** UTF-8 or UTF-16 (see above). If the next parameter, "nBytes", is less
** than zero, then zSql is read up to the first nul terminator.  If
** "nBytes" is not less than zero, then it is the length of the string zSql
** in bytes (not characters).
**
** *pzTail is made to point to the first byte past the end of the first
** SQL statement in zSql.  This routine only compiles the first statement
** in zSql, so *pzTail is left pointing to what remains uncompiled.
**
** *ppStmt is left pointing to a compiled SQL statement that can be
** executed using sqlite3_step().  Or if there is an error, *ppStmt may be
** set to NULL.  If the input text contained no SQL (if the input is and
** empty string or a comment) then *ppStmt is set to NULL.
**
** On success, SQLITE_OK is returned.  Otherwise an error code is returned.
*/

typedef int (* PFN_Sqlite3_Prepare)(
									sqlite3 *db,            /* Database handle */
									const char *zSql,       /* SQL statement, UTF-8 encoded */
									int nBytes,             /* Length of zSql in bytes. */
									sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
									const char **pzTail     /* OUT: Pointer to unused portion of zSql */
									);
typedef int (*PFN_Sqlite3_Prepare16)(
									 sqlite3 *db,            /* Database handle */
									 const void *zSql,       /* SQL statement, UTF-16 encoded */
									 int nBytes,             /* Length of zSql in bytes. */
									 sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
									 const void **pzTail     /* OUT: Pointer to unused portion of zSql */
									 );

/*
** In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(),
** one or more literals can be replace by parameters "?" or ":AAA" or
** "$VVV" where AAA is an identifer and VVV is a variable name according
** to the syntax rules of the TCL programming language.
** The value of these parameters (also called "host parameter names") can
** be set using the routines listed below.
**
** In every case, the first parameter is a pointer to the sqlite3_stmt
** structure returned from sqlite3_prepare().  The second parameter is the
** index of the parameter.  The first parameter as an index of 1.  For
** named parameters (":AAA" or "$VVV") you can use 
** sqlite3_bind_parameter_index() to get the correct index value given
** the parameters name.  If the same named parameter occurs more than
** once, it is assigned the same index each time.
**
** The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and
** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
** text after SQLite has finished with it.  If the fifth argument is the
** special value SQLITE_STATIC, then the library assumes that the information
** is in static, unmanaged space and does not need to be freed.  If the
** fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its
** own private copy of the data.
**
** The sqlite3_bind_* routine must be called before sqlite3_step() after
** an sqlite3_prepare() or sqlite3_reset().  Unbound parameterss are
** interpreted as NULL.
*/

typedef int ( *PFN_Sqlite3_bind_blob)(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
typedef int ( *PFN_Sqlite3_bind_double)(sqlite3_stmt*, int, double);
typedef int ( *PFN_Sqlite3_bind_int)(sqlite3_stmt*, int, int);
typedef int ( *PFN_Sqlite3_bind_int64)(sqlite3_stmt*, int, sqlite_int64);
typedef int ( *PFN_Sqlite3_bind_null)(sqlite3_stmt*, int);
typedef int ( *PFN_Sqlite3_bind_text)(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
typedef int ( *PFN_Sqlite3_bind_text16)(sqlite3_stmt*, int, const void*, int, void(*)(void*));
typedef int ( *PFN_Sqlite3_bind_value)(sqlite3_stmt*, int, const sqlite3_value*);


/*
** Return the number of columns in the result set returned by the compiled
** SQL statement. This routine returns 0 if pStmt is an SQL statement
** that does not return data (for example an UPDATE).
*/
/*int sqlite3_column_count(sqlite3_stmt *pStmt);*/
typedef int (*PFN_Sqlite3_column_count)(sqlite3_stmt *pStmt);

/*
** The first parameter is a compiled SQL statement. This function returns
** the column heading for the Nth column of that statement, where N is the
** second function parameter.  The string returned is UTF-8 for
** sqlite3_column_name() and UTF-16 for sqlite3_column_name16().
*/
typedef const char *( *PFN_Sqlite3_column_name )(sqlite3_stmt*,int);
typedef const void *( *PFN_Sqlite3_column_name16)(sqlite3_stmt*,int);


/* 
** After an SQL query has been compiled with a call to either
** sqlite3_prepare() or sqlite3_prepare16(), then this function must be
** called one or more times to execute the statement.
**
** The return value will be either SQLITE_BUSY, SQLITE_DONE, 
** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
**
** SQLITE_BUSY means that the database engine attempted to open
** a locked database and there is no busy callback registered.
** Call sqlite3_step() again to retry the open.
**
** SQLITE_DONE means that the statement has finished executing
** successfully.  sqlite3_step() should not be called again on this virtual
** machine.
**
** If the SQL statement being executed returns any data, then 
** SQLITE_ROW is returned each time a new row of data is ready
** for processing by the caller. The values may be accessed using
** the sqlite3_column_*() functions described below. sqlite3_step()
** is called again to retrieve the next row of data.
** 
** SQLITE_ERROR means that a run-time error (such as a constraint
** violation) has occurred.  sqlite3_step() should not be called again on
** the VM. More information may be found by calling sqlite3_errmsg().
**
** SQLITE_MISUSE means that the this routine was called inappropriately.
** Perhaps it was called on a virtual machine that had already been
** finalized or on one that had previously returned SQLITE_ERROR or
** SQLITE_DONE.  Or it could be the case the the same database connection
** is being used simulataneously by two or more threads.
*/
/*int sqlite3_step(sqlite3_stmt*);*/
typedef int (*PFN_Sqlite3_step )(sqlite3_stmt*);

/*
** Return the number of values in the current row of the result set.
**
** After a call to sqlite3_step() that returns SQLITE_ROW, this routine
** will return the same value as the sqlite3_column_count() function.
** After sqlite3_step() has returned an SQLITE_DONE, SQLITE_BUSY or
** error code, or before sqlite3_step() has been called on a 
** compiled SQL statement, this routine returns zero.
*/
/*int sqlite3_data_count(sqlite3_stmt *pStmt);*/
typedef int (*PFN_Sqlite3_data_count )(sqlite3_stmt *pStmt);


/*
** The next group of routines returns information about the information
** in a single column of the current result row of a query.  In every
** case the first parameter is a pointer to the SQL statement that is being
** executed (the sqlite_stmt* that was returned from sqlite3_prepare()) and
** the second argument is the index of the column for which information 
** should be returned.  iCol is zero-indexed.  The left-most column as an
** index of 0.
**
** If the SQL statement is not currently point to a valid row, or if the
** the colulmn index is out of range, the result is undefined.
**
** These routines attempt to convert the value where appropriate.  For
** example, if the internal representation is FLOAT and a text result
** is requested, sprintf() is used internally to do the conversion
** automatically.  The following table details the conversions that
** are applied:
**
**    Internal Type    Requested Type     Conversion
**    -------------    --------------    --------------------------
**       NULL             INTEGER         Result is 0
**       NULL             FLOAT           Result is 0.0
**       NULL             TEXT            Result is an empty string
**       NULL             BLOB            Result is a zero-length BLOB
**       INTEGER          FLOAT           Convert from integer to float
**       INTEGER          TEXT            ASCII rendering of the integer
**       INTEGER          BLOB            Same as for INTEGER->TEXT
**       FLOAT            INTEGER         Convert from float to integer
**       FLOAT            TEXT            ASCII rendering of the float
**       FLOAT            BLOB            Same as FLOAT->TEXT
**       TEXT             INTEGER         Use atoi()
**       TEXT             FLOAT           Use atof()
**       TEXT             BLOB            No change
**       BLOB             INTEGER         Convert to TEXT then use atoi()
**       BLOB             FLOAT           Convert to TEXT then use atof()
**       BLOB             TEXT            Add a \000 terminator if needed
**
** The following access routines are provided:
**
** _type()     Return the datatype of the result.  This is one of
**             SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB,
**             or SQLITE_NULL.
** _blob()     Return the value of a BLOB.
** _bytes()    Return the number of bytes in a BLOB value or the number
**             of bytes in a TEXT value represented as UTF-8.  The \000
**             terminator is included in the byte count for TEXT values.
** _bytes16()  Return the number of bytes in a BLOB value or the number
**             of bytes in a TEXT value represented as UTF-16.  The \u0000
**             terminator is included in the byte count for TEXT values.
** _double()   Return a FLOAT value.
** _int()      Return an INTEGER value in the host computer's native
**             integer representation.  This might be either a 32- or 64-bit
**             integer depending on the host.
** _int64()    Return an INTEGER value as a 64-bit signed integer.
** _text()     Return the value as UTF-8 text.
** _text16()   Return the value as UTF-16 text.
*/
//const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
//int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
//int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
//double sqlite3_column_double(sqlite3_stmt*, int iCol);
//int sqlite3_column_int(sqlite3_stmt*, int iCol);
//sqlite_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
//const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
//const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
//int sqlite3_column_type(sqlite3_stmt*, int iCol);

typedef const void * (*PFN_Sqlite3_column_blob)(sqlite3_stmt*, int iCol);
typedef int (*PFN_Sqlite3_column_bytes)(sqlite3_stmt*, int iCol);
typedef int (*PFN_Sqlite3_column_bytes16)(sqlite3_stmt*, int iCol);
typedef double (*PFN_Sqlite3_column_double)(sqlite3_stmt*, int iCol);
typedef int (*PFN_Sqlite3_column_int)(sqlite3_stmt*, int iCol);
typedef sqlite_int64 (*PFN_Sqlite3_column_int64 )(sqlite3_stmt*, int iCol);
typedef const unsigned char *(*PFN_Sqlite3_column_text )(sqlite3_stmt*, int iCol);
typedef const void *(*PFN_Sqlite3_column_text16)(sqlite3_stmt*, int iCol);
typedef int (*PFN_Sqlite3_column_type)(sqlite3_stmt*, int iCol);

/*
** The sqlite3_finalize() function is called to delete a compiled
** SQL statement obtained by a previous call to sqlite3_prepare()
** or sqlite3_prepare16(). If the statement was executed successfully, or
** not executed at all, then SQLITE_OK is returned. If execution of the
** statement failed then an error code is returned. 
**
** This routine can be called at any point during the execution of the
** virtual machine.  If the virtual machine has not completed execution
** when this routine is called, that is like encountering an error or
** an interrupt.  (See sqlite3_interrupt().)  Incomplete updates may be
** rolled back and transactions cancelled,  depending on the circumstances,
** and the result code returned will be SQLITE_ABORT.
*/
/*int sqlite3_finalize(sqlite3_stmt *pStmt);*/
typedef int (*PFN_Sqlite3_Finalize)(sqlite3_stmt *pStmt);


/*
** The sqlite3_reset() function is called to reset a compiled SQL
** statement obtained by a previous call to sqlite3_prepare() or
** sqlite3_prepare16() back to it's initial state, ready to be re-executed.
** Any SQL statement variables that had values bound to them using
** the sqlite3_bind_*() API retain their values.
*/
/*int sqlite3_reset(sqlite3_stmt *pStmt);*/
typedef int (*PFN_Sqlite3_reset)(sqlite3_stmt *pStmt);


typedef int (*PFN_Sqlite3_key)(sqlite3 *db,const void* pKey,int nKey);
typedef int (*PFN_Sqlite3_rekey)(sqlite3 *db,const void* pKey,int nKey);

#endif
